import global, {VERSION } from "../global";
import Matching from "./matching";

const {ccclass, property} = cc._decorator;

@ccclass
export default class HallClient extends cc.Component {

    @property(cc.Label)
    label: cc.Label = null;             //大厅在线人数
    @property(cc.Label)
    label_playerId: cc.Label = null;    
    @property(cc.Node)
    node_matching:cc.Node = null;       //为了获取 matching.ts

    static arr_data:Array<any> = new Array();

    onLoad () {
        global.hallClient = this;
        HallClient.arr_data.forEach(data=>{
            this.onMessage(data)
        })
        let obj = {other:"check_version","version":VERSION};
        global.websocket.send(JSON.stringify(obj));
    }

    /**
     * 收到消息
     */
    onMessage(data:any){
        if(cc.director.getScene().name!="index"){
            return
        }
        switch(data.other){
            case "numOfHallPlayers":
                this.label.string = "大厅在线人数: "+data.numOfHallPlayers
                break;
            case "playerId":
                global.id = data.id;
                this.label_playerId.string = "玩家"+data.id+"号"
                break;
            case "matching_success":
                global.seat = data.seat;
                global.hallClient = null;
                cc.director.loadScene("game");
                break;
            case "check_version":
                global.is_checkVersion = data.result;
                if(!data.result){
                    global.persist.tip("请更新游戏版本");
                }
                break;
            default :console.error("未知other: ",data.other);
        }
    }

}
